﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Image2d
{
    class AsteroidsManager
    {
        Vector2 position;
        Texture2D image;
        Animation[] animation;

        float rotation;
        float velocity;
        float nextMeteor = 0;
        float interval = 2000f;

        Vector2 clientbounds;

        List<Asteroids> asteroids;
        public List<Asteroids> Asteroides
        {
            get { return asteroids; }
            set { asteroids = value; }
        }

        public AsteroidsManager(Texture2D image,
            Animation[] animation, Vector2 bounds)
        {
            this.image = image;
            this.animation = animation;
            
            clientbounds = bounds;
            velocity = 1f;
            position = new Vector2(-animation[0].frameSize.X, 
                                   -animation[0].frameSize.Y) +new Vector2(250, 250);
            rotation = 0f;

            asteroids = new List<Asteroids>();
        }


 /**/   public void Update(GameTime gameTime)
        {
            //logica de spawn de asteroides
            //TODO
            if (nextMeteor < (float)gameTime.TotalGameTime.TotalMilliseconds)
            {             
                Random r = new Random();
                int rand = r.Next(8);
                StartPosition(rand);
                //MathHelper.PiOver4 = 45º em radianos ou seja 1/8 da circunferencia
                rotation = rand * MathHelper.PiOver4;
                //Console.WriteLine("Meteor Shower" + rand);
                asteroids.Add(new Asteroids(image, position, animation,
                    rotation, velocity, clientbounds));
                nextMeteor = (float)gameTime.TotalGameTime.TotalMilliseconds +interval;
            }



/**/        for (int i = asteroids.Count - 1; i >= 0; i--)
            {
                //O gameTime atualiza a animação. Quando o gameTime inicializa muito
                //alto a animação corre direto ate alcançar o valor do game time
                //por isso vamos pasar o gameTime atual pro updateTime da animacao
                asteroids[i].Animations[0].updateTime = (float)gameTime.TotalGameTime.TotalMilliseconds;
                asteroids[i].Update(gameTime);
                if(asteroids[i].CheckUpdate(gameTime) )
                {
                    asteroids.RemoveAt(i);
                }
            }
        }

/**/    private void StartPosition(int rand)
        {
            switch (rand)
            {
                case 0:     //anda pra cima
                    position = new Vector2(clientbounds.X / 2, clientbounds.Y + animation[0].frameSize.Y/2 );
                    break;
                case 1:     //anda pra direita superior
                    position = new Vector2(0 - animation[0].frameSize.X / 2, clientbounds.Y + animation[0].frameSize.Y / 2);
                    break; 
                case 2:     //anda pra direita
                    position = new Vector2(0 - animation[0].frameSize.X / 2, clientbounds.Y / 2);
                    break;
                case 3:     //anda pra direita inferior
                    position = new Vector2(0 - animation[0].frameSize.X / 2, 0 - animation[0].frameSize.Y / 2);
                    break;
                case 4:     //anda pra baixo
                    position = new Vector2(clientbounds.X / 2, 0 - animation[0].frameSize.Y / 2);
                    break;
                case 5:     //anda pra esquerda inferior
                    position = new Vector2(clientbounds.X + animation[0].frameSize.X / 2, 0 - animation[0].frameSize.Y / 2);
                    break;
                case 6:     //anda pra esquerda
                    position = new Vector2(clientbounds.X + animation[0].frameSize.X / 2, clientbounds.Y / 2);
                    break;
                case 7:     //anda pra esquerda superior
                    position = new Vector2(clientbounds.X + animation[0].frameSize.X / 2, clientbounds.Y + animation[0].frameSize.Y / 2);
                    break;

            }
        }


        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            for (int i = asteroids.Count - 1; i >= 0; i--)
            {
                asteroids[i].Draw(spriteBatch, gameTime);
            }
        }
    }
}
